﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Axe.Geometry
{
    public class Line2d
    {
        Vector2 m_Start = Vector2.Zero;
        Vector2 m_End = Vector2.Zero;

        public Vector2 Start
        {
            get { return m_Start; }
            set { m_Start = value; }
        }

        public Vector2 End
        {
            get { return m_End; }
            set { m_End = value; }
        }


        public Line2d()
        {
        }

        public Line2d(Vector2 start, Vector2 end)
        {
            Start = start.Clone();
            End = end.Clone();
        }


        #region public Vector2 GetPoint(double t)
        /// <summary>
        /// Возвращает точку, лежащую на отрезке, заданном параметрами <paramref name="StartPoint"/> и <paramref name="EndPoint"/>.
        /// </summary>
        /// <param name="t">Число, в интервале [0..1], где 0 соответствует <paramref name="StartPoint"/>, а 1 - <paramref name="EndPoint"/></param>
        /// <returns></returns>
        public static Vector2 GetPoint(Vector2 start, Vector2 end, double t)
        {
            if (start == null || end == null)
            {
                return null;
            }

            double length = Vector2.Distance(start, end);
            Vector2 unit = (end - start).GetUnit();

            return start + unit * t * length;
        }
        #endregion

        public static void GetLineCanonical(Vector2 p1, Vector2 p2, out double A, out double B, out double C)
        {
            A = 1.0 / (p2.X - p1.X);
            B = -1.0 / (p2.Y - p1.Y);
            C = p1.Y / (p2.Y - p1.Y) - p1.X / (p2.X - p1.X);
        }


        public static Vector2 ProjectOrthoOnLine(IReadOnlyVector2 a, IReadOnlyVector2 b, IReadOnlyVector2 c)
        {
            Vector2 ab = new Vector2(b) - new Vector2(a);
            Vector2 proj = Vector2.ProjectBtoA(ab, new Vector2(c) - new Vector2(a));
            return new Vector2(a) + proj;
        }
    }
}
